﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;

public class UIController : MonoBehaviour {

    public GameObject countdown = null;
    public GameObject placeButton = null;

    public GameObject readyPlayer1 = null;

    public GameObject hider = null;
    public GameObject seeker = null;

    private bool countdownStarted = false;
    private float time = 0;
    private int startingTime = 0;

    private bool initialized = false;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
        if (GameState.Instance.GetState() == State.HidingPhase)
        {
            if (!initialized)
            {
                readyPlayer1.SetActive(false);
                initialized = true;
            }
            StartCountdown();
            if (countdownStarted)
            {
                if (time == 0)
                    time = Time.time;
                else
                {
                    float timeLeft = startingTime - time - Time.time;
                    if (timeLeft > 0)
                        countdown.GetComponent<Text>().text = Math.Ceiling(timeLeft).ToString();
                    else
                    {
                        time = 0;
                        countdownStarted = false;
                        //GameState.Instance.SetStateToSeekingPhase();
                    }
                    //Debug.Log(Math.Ceiling(startingTime - time - Time.time).ToString());
                    //Debug.Log((startingTime - time - Time.time).ToString());
                }
            }
        }
    }

    private void StartCountdown()
    {
        if (!countdownStarted)
        {
            startingTime = DataStorage.Instance.GetCountdownBeginning();
            countdownStarted = true;
        }
    }

    public void PlaceFurniture()
    {
        hider.GetComponent<Hider>().enabled = false;
        GameState.Instance.SetStateToSeekingPhase();

        seeker.SetActive(true);
    }
}
